from utils.excel_util import ExcelUtil
from model.sysDeptModel import SysDept
from schema.sysDeptSchema import SysDeptCreate
from typing import List, Any, Dict, ClassVar
from io import BytesIO
from datetime import datetime


class SysDeptExcel:
    # Define export columns
    COLUMNS: ClassVar[List[Dict[str, Any]]] = [

        {"field": "parent_id", "title": "父部门id", "required": False},

        {"field": "ancestors", "title": "祖级列表", "required": False},

        {"field": "dept_name", "title": "部门名称", "required": False},

        {"field": "order_num", "title": "显示顺序", "required": False},

        {"field": "leader", "title": "负责人", "required": False},

        {"field": "phone", "title": "联系电话", "required": False},

        {"field": "email", "title": "邮箱", "required": False},

        {"field": "status", "title": "状态（0代表存在 2代表删除）", "required": False},

        {"field": "use_flag", "title": "部门状态（0正常 1停用）", "required": False},

        {"field": "create_by", "title": "创建者", "required": False},

        {"field": "create_time", "title": "创建时间", "required": False},

        {"field": "update_by", "title": "更新者", "required": False},

        {"field": "update_time", "title": "更新时间", "required": False},

    ]

    @classmethod
    def get_export_columns(cls) -> List[Dict[str, Any]]:
        return [

            {"field": "parent_id", "title": "父部门id"},

            {"field": "ancestors", "title": "祖级列表"},

            {"field": "dept_name", "title": "部门名称"},

            {"field": "order_num", "title": "显示顺序"},

            {"field": "leader", "title": "负责人"},

            {"field": "phone", "title": "联系电话"},

            {"field": "email", "title": "邮箱"},

            {"field": "status", "title": "状态（0代表存在 2代表删除）"},

            {"field": "use_flag", "title": "部门状态（0正常 1停用）"},

            {"field": "create_by", "title": "创建者"},

            {"field": "create_time", "title": "创建时间"},

            {"field": "update_by", "title": "更新者"},

            {"field": "update_time", "title": "更新时间"},

        ]

    @classmethod
    def get_import_template(cls) -> BytesIO:
        """Get import template"""
        example_data = [{

            "parent_id": 1,

            "ancestors": "example",

            "dept_name": "example",

            "order_num": 1,

            "leader": "example",

            "phone": "example",

            "email": "example",

            "status": "A",

            "use_flag": "A",

            "create_by": "example",

            "create_time": datetime.now(),

            "update_by": "example",

            "update_time": datetime.now(),

        }]
        return ExcelUtil.create_import_template(
            columns=cls.COLUMNS,
            example_data=example_data,
            sheet_name="部门表导入模板"
        )

    @classmethod
    async def export_data(cls, data: List[Any]) -> BytesIO:
        """Export data"""
        return await ExcelUtil.export_data(
            data=data,
            columns=cls.get_export_columns(),
            sheet_name="部门表数据"
        )

    @classmethod
    def parse_import_data(cls, file: BytesIO) -> List[Dict[str, Any]]:
        """Parse import data"""
        return ExcelUtil.parse_import_data(
            file=file,
            columns=cls.COLUMNS,
            schema_cls=SysDeptCreate
        )